#include "mainwidget.h"
#include <QMessageBox>
#include <QHostInfo>
#include <QDebug>

mainWidget::mainWidget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("Tcp Client"));
    contentListWidget = new QListWidget;
    sendLineEdit = new QLineEdit;
    sendBtn = new QPushButton(tr("Send"));
    userNameLabel = new QLabel(tr("Name:"));
    userNameLineEdit = new QLineEdit;
    serverIpLabel = new QLabel(tr("Server Ip:"));
    serverIpLineEdit = new QLineEdit;
    portLabel = new QLabel(tr("Port:"));
    portLineEdit = new QLineEdit;
    portLineEdit->setInputMask("9999");
    enterBtn = new QPushButton(tr("Enter"));

    //总体布局
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(contentListWidget, 0 , 0, 1, 2);
    mainLayout->addWidget(sendLineEdit, 1, 0);
    mainLayout->addWidget(sendBtn, 1, 1);
    mainLayout->addWidget(userNameLabel, 2, 0);
    mainLayout->addWidget(userNameLineEdit, 2, 1);
    mainLayout->addWidget(serverIpLabel, 3, 0);
    mainLayout->addWidget(serverIpLineEdit, 3, 1);
    mainLayout->addWidget(portLabel, 4, 0);
    mainLayout->addWidget(portLineEdit, 4, 1);
    mainLayout->addWidget(enterBtn, 5, 0);

    status = false;
    port = 8010;
    portLineEdit->setText(QString::number(port));
    serverIp = new QHostAddress();

    connect(enterBtn, SIGNAL(clicked()), this, SLOT(slotEnter()));
    connect(sendBtn, SIGNAL(clicked()), this, SLOT(slotSend()));
    sendBtn->setEnabled(false);

}

mainWidget::~mainWidget()
{
}

void mainWidget::slotEnter()
{
    if(!status)
    {
        //完成输入合法性检验
        QString ip = serverIpLineEdit->text();
        if(!serverIp->setAddress(ip))
        {
            QMessageBox::information(this, tr("error"), tr("Server ip address error!"));
            return;
        }
        if(userNameLineEdit->text() == "")
        {
            QMessageBox::information(this, tr("error"), tr("User name error!"));
            return;
        }
        //获取端口
        port = portLineEdit->text().toInt();
        userName = userNameLineEdit->text();

        //创建一个 QTcpSocket 对象，将信号和槽关联起来
        tcpsocket = new QTcpSocket(this);
        connect(tcpsocket, SIGNAL(connected()), this, SLOT(slotConnected()));
        connect(tcpsocket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
        connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));
        tcpsocket->connectToHost(*serverIp, port);
        status = true;
    }
    else {
        int length = 0;
        QString msg = userName + tr(": Leave Chat Room!");
        if((length = tcpsocket->write(msg.toLatin1(), msg.length())) != msg.length())
        {
            qDebug() << "length error!";
            return;
        }
        tcpsocket->disconnectFromHost();
        status = false;
    }
}

void mainWidget::slotConnected()
{
    sendBtn->setEnabled(true);
    enterBtn->setText(tr("Quit"));
    int length = 0;
    QString msg = userName + tr(": Enter Chat Room!");
    if((length = tcpsocket->write(msg.toLatin1(), msg.length())) != msg.length())
    {
        //发送信息不完全，不作处理
        return;
    }
}

void mainWidget::slotSend()
{
    if(sendLineEdit->text() == "")
    {
        //发送消息为空
        return ;
    }
    QString msg = userName + tr(" : ") + sendLineEdit->text();
    tcpsocket->write(msg.toLatin1(), msg.length());
    sendLineEdit->clear();
}

void mainWidget::slotDisconnected()
{
    sendBtn->setEnabled(false);
    enterBtn->setText(tr("Enter Chat Room"));
}

void mainWidget::dataReceived()
{
    //处理接收数据
    while (tcpsocket->bytesAvailable() > 0) {
        //有效数据
        QByteArray datagram;
        datagram.resize(tcpsocket->bytesAvailable());
        tcpsocket->read(datagram.data(), datagram.size());
        QString msg = datagram.data();
        contentListWidget->addItem(msg.left(datagram.size()));
    }
}
